package com.ncmmall.mall.controller.business.setting;

import com.ncmmall.application.business.setting.SystemSettingApplication;
import com.ncmmall.application.securitys.logs.OperationLogApplication;
import com.ncmmall.domain.business.setting.SystemSetting;
import com.ncmmall.domain.business.setting.SystemSettingRecord;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Map;

/**
 * @author likaikai
 * @description 用户注册协议
 * @date 2019-07-06 14:39
 */
@Controller
@RequestMapping("/mall/setting/registerProtocol")
public class RegisterProtocolController {

    private Logger logger = LoggerFactory.getLogger(RegisterProtocolController.class);

    @Autowired
    private SystemSettingApplication settingApplication;

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private OperationLogApplication logApplication;

    /**
     * 查看系统设置
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Model model) {
        SystemSetting setting = queryChannelService.queryOneEntity("select * from mall_system_setting limit 1", Collections.emptyMap(), SystemSetting.class);
        if (setting == null) {
            setting = new SystemSetting();
        }
        model.addAttribute("setting", setting);
        return "mall/setting/registerProtocol/detail";
    }

    /**
     * 修改积分设置
     *
     * @param setting
     * @param type
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> modify(HttpServletRequest request, SystemSetting setting) {
        try {
            settingApplication.modify(setting, SystemSetting.MEMBER_PROTOCOL, Securitys.currentUser());
            logApplication.eventOfSystem(Securitys.currentUser(), "修改注册协议", Securitys.getIp(request));
            return ImmutableMap.of("status", true, "msg", "修改成功");
        } catch (Exception e) {
            logger.error("修改系统设置异常", e);
            return ImmutableMap.of("status", false, "msg", "修改失败");
        }
    }


    /**
     * 修改记录
     *
     * @return
     */
    @RequestMapping(value = "/record", method = RequestMethod.GET)
    public String record(Model model, PageParams pageParams) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo()).setPageSize(pageParams.getPageSize());
        QueryPage<SystemSettingRecord> page = queryChannelService.queryPageEntity("select * from mall_system_setting_record where type = :type order by createTime desc", pageRequest.setParams(ImmutableMap.of("type", SystemSetting.MEMBER_PROTOCOL)), SystemSettingRecord.class);
        model.addAttribute("page", page);
        return "mall/setting/registerProtocol/record";
    }

}
